﻿using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Data.Entity;
using System.Linq;
using System.Web;

namespace TheCup
{
    [Table("events")]
    [JsonObject(IsReference = true)]
    public class Event
    {
        [Key]
        [Column("no")]
        public int Id { get; set; }

        [Column("description")]
        [Display(Name="navn")]
        public string Name { get; set; }

        [Column("moreinformation")]
        public string Content { get; set; }

        [NotMapped]
        public string ContentAsHtml
        {
            get
            {
                string value = this.Content;
                value = value.Replace(Environment.NewLine, "<br>");
                return value;
            }
        }

        public Nullable<int> EventTypeId { get; set; }

        [ForeignKey("EventTypeId")]
        public virtual EventType Type { get; set; }

        [Column("courseno")]
        public Nullable<int> CourseId { get; set; }
        
        [ForeignKey("CourseId")]
        [Display(Name = "bane")]
        public virtual Course Course { get; set; }

        public string Location { get; set; }

        [Column("StartingTimeIsBooked")]
        public Nullable<bool> IsTeeTimeBooked { get; set; }

        public bool IsMajor { get; set; }

        [Column("EventMeetingTime")]
        [DisplayFormat(DataFormatString = "Mødetid: {0:hh\\:mm}", NullDisplayText = "Mødetid ubestemt")]
        [Display(Name="mødetid")]
        public Nullable<TimeSpan> MeetingTime { get; set; }
        
        [Column("EventStartingTime")]
        [DisplayFormat(DataFormatString = "Starttid: {0:hh\\:mm}", NullDisplayText = "Starttid ubestemt")]
        [Display(Name = "starttid")]
        public Nullable<TimeSpan> StartingTime { get; set; }

        [DisplayFormat(ApplyFormatInEditMode = true)]
        public Nullable<int> EntryFee { get; set; }

        [Column("eventdate")]
        [DataType(DataType.Date)]
        [Display(Name="dato")]
        public DateTime StartDate { get; set; }
        
        [Column("eventenddate")]
        [DataType(DataType.Date)]
        public Nullable<DateTime> EndDate { get; set; }
        
        [Column("lastjoindate")]
        [DataType(DataType.Date)]
        [Display(Name="tilmeld pr.")]
        public Nullable<DateTime> JoinByDate { get; set; }

        [ForeignKey("PlayerId")]
        public Player Organizer { get; set; }
        public Nullable<int> PlayerId { get; set; }
        
        public int NumberOfAttendees
        {
            get
            {
                return this.Joins == null ? 0 : this.Joins.Count;
            }
        }

        public string NameForDropdown
        {
            get
            {
                return string.Format("{0} ({1})", this.Name, this.StartDate.ToLongDateString());
            }
        }

        public virtual List<EventJoin> Joins { get; set; }

        [Obsolete]
        [Column("Id")]
        public Guid uuid { get; set; }

        [Obsolete]
        [Column("OwnerUserId")]
        public Guid LegacyOwnerUuid { get; set; }

        [Obsolete]
        [Column("mastercourseid")]
        public Nullable<Guid> LegacyCourseUuid { get; set; }

        [Column("datecreated")]
        [DataType(DataType.Date)]
        public DateTime WhenCreated { get; set; }

        [DataType(DataType.Date)]
        public Nullable<DateTime> DateDeleted { get; set; }

        [NotMapped]
        public CommentsViewModel Comments { get; set; }

        public bool IsExpired
        {
            get
            {
                return this.DateDeleted != null && (this.DateDeleted.GetValueOrDefault() < DateTime.UtcNow);
            }
        }

        public void Expire()
        {
            this.DateDeleted = DateTime.UtcNow;
        }

        public void ApplyRules()
        {
            this.Name = this.Name.Trim();
            this.Content = this.Content.Trim();
            this.Location = !string.IsNullOrEmpty(this.Location) ? this.Location.Trim() : this.Location;

            if (this.JoinByDate == null) { this.JoinByDate = this.StartDate; }
            if (this.JoinByDate < this.StartDate) { this.JoinByDate = this.StartDate.AddDays(-1); }
            if (this.CourseId == null)
            {
                this.LegacyCourseUuid = null;
            }
            else
            {
                ApiControllers.CourseController c = new ApiControllers.CourseController();
                Course course = c.Get(this.CourseId.GetValueOrDefault());
                if (course != null)
                {
                    this.LegacyCourseUuid = course.uuid;
                }
            }
            if (this.EndDate == null) { this.EndDate = this.StartDate; }
            if (this.IsTeeTimeBooked == null) { this.IsTeeTimeBooked = false; }
        }
    }
}